import 'package:flutter/material.dart';
import 'hero.dart';

void main() {
  runApp(const MyApp());
}

var routes = {
  "/hero": (context,{arguments}) => HeroPage(arguments: arguments),
};

var onGenerateRoute = (RouteSettings settings) {
  final String? name = settings.name;
  final Function? pageContentBuilder = routes[name];
  if (pageContentBuilder != null) {
    if (settings.arguments != null) {
      final Route route = MaterialPageRoute(builder: (context) {
        return pageContentBuilder(context, arguments: settings.arguments);
      });
      return route;
    } else {
      final Route route = MaterialPageRoute(builder: (context) =>  pageContentBuilder(context));
      return route;
    }
  }
};

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        useMaterial3: false,
        primarySwatch: Colors.yellow,
      ),
      home: Scaffold(
        appBar: AppBar(title: const Text("Flutter App")),
        body: const MyHomePage(),
      ),
      onGenerateRoute: onGenerateRoute,
    );
  }
}



class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<String> list = [];


  @override
  void initState() {
    super.initState();
    list =  const [
      "https://www.itying.com/images/flutter/1.png",
      "https://www.itying.com/images/flutter/2.png",
      "https://www.itying.com/images/flutter/3.png",
      "https://www.itying.com/images/flutter/4.png",
      "https://www.itying.com/images/flutter/5.png",
      "https://www.itying.com/images/flutter/6.png",
    ];
  }

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        itemCount: list.length,
        padding: const EdgeInsets.all(10),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 2,    //一行widget个数
          mainAxisSpacing: 2,  //上下间距
          crossAxisSpacing: 10, //水平间距
        ),
        itemBuilder: (ctx, index) {
          return GestureDetector(
            onTap: (){
              Navigator.pushNamed(context, "/hero",arguments: {
                "imageUrl" : list[index],
                "listData" : list,
                "initialPage": index,
              });
            },
            child: Column(
              children: [
                Image(image: NetworkImage(list[index]),fit: BoxFit.cover,),
                const SizedBox(height: 10,),
                const Text("我是标题",style: TextStyle(fontSize: 16),),
              ],
            ),
          );
        });
  }
}
